<?php
namespace Admin\Model;
use Think\Model;
class GoodsModel extends Model 
{
	protected $insertFields = array('goods_name','goods_desc','goods_price','goods_reserve','goods_thumb','goods_addtion_desc','goodsname_en','category');
	protected $updateFields = array('gid','goods_name','goods_desc','goods_price','goods_reserve','goods_thumb','goods_addtion_desc','goodsname_en','category');
	protected $_validate = array(
		array('goods_name', 'require', '商品名称不能为空！', 1, 'regex', 3),
		array('goods_name', '1,100', '商品名称的值最长不能超过 100 个字符！', 1, 'length', 3),
		array('goods_desc', 'require', '商品描述不能为空！', 1, 'regex', 3),
		array('goods_desc', '1,255', '商品描述的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('goods_price', 'require', '商品价格不能为空！', 1, 'regex', 3),
		array('goods_price', 'currency', '商品价格必须是货币格式！', 1, 'regex', 3),
		array('goods_reserve', 'require', '库存量不能为空！', 1, 'regex', 3),
		array('goods_reserve', 'number', '库存量必须是一个整数！', 1, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($goods_name = I('get.goods_name'))
			$where['goods_name'] = array('like', "%$goods_name%");
		if($goods_desc = I('get.goods_desc'))
			$where['goods_desc'] = array('like', "%$goods_desc%");
		$goods_pricefrom = I('get.goods_pricefrom');
		$goods_priceto = I('get.goods_priceto');
		if($goods_pricefrom && $goods_priceto)
			$where['goods_price'] = array('between', array($goods_pricefrom, $goods_priceto));
		elseif($goods_pricefrom)
			$where['goods_price'] = array('egt', $goods_pricefrom);
		elseif($goods_priceto)
			$where['goods_price'] = array('elt', $goods_priceto);
		if($goods_reserve = I('get.goods_reserve'))
			$where['goods_reserve'] = array('eq', $goods_reserve);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, 5);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.gid')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['goods_pic']) && $_FILES['goods_pic']['error'] == 0)
		{
			$ret = uploadOne('goods_pic', 'Goods', array(
				array(350, 350, 2),
//				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['goods_pic'] = $ret['images'][0];
				$data['goods_thumb'] = $ret['images'][1];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		/********************************* 添加商品图片 ************************************/

		if(isset($_FILES['goods_pic']) && $_FILES['goods_pic']['error'] == 0)
		{
			$ret = uploadOne('goods_pic', 'Goods', array(
				array(350, 350, 2),
				//array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['goods_pic'] = $ret['images'][0];
				$data['goods_thumb'] = $ret['images'][1];
			}
			else
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.goods_pic'),
				I('post.goods_thumb'),
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['gid']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('goods_pic,goods_thumb')->find($option['where']['gid']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}